<?php
/*
*登录与注销
**/
class HomeController{
	public static $ci;
	public static $view;
	public static $db;
	function __construct($ci){		
		self::$ci=$ci;
		self::$view=$ci['view'];
		self::$db=$ci['db'];
	}
	function index($req,$res){
		$queryParams=$req->getQueryParams();
		$msg='';
		$from='/disease';
		if(!empty($queryParams['from'])){
			$from=$queryParams['from'];
		}
		if($req->isPost()){
			$params=$req->getParsedBody();
			if(!empty($params['email'])&&!empty($params['password'])){
				$wp_user=self::$db->get('users','*',['user_email'=>$params['email']]);
				$password = $params['password'];  
				$wp_hasher = new PasswordHash(8, TRUE);  
				$data=$wp_hasher->CheckPassword($password,$wp_user['user_pass']);
				if($data){
					$session=new UserSession();
					$session->set($wp_user);
					if(!empty($params['from'])){
						$from=$params['from'];
					}
					return $res->withRedirect($from, 301);
				}else{
					$msg='邮箱或者密码错误!';
				}
			}
			
		}


		return self::$view->render('index.twig', array('title' => '登录','msg'=>$msg,'from'=>$from));
	}


	function logout(){
		$session=new UserSession();
		$session->destroy();
		return self::$view->render('index.twig', array('title' => '成功注销登录'));
	}

}